//
//  WEPopoverContentViewController.m
//  WEPopover
//
//  Created by Werner Altewischer on 06/11/10.
//  Copyright 2010 Werner IT Consultancy. All rights reserved.
//

#import "SearchTypeViewController.h"
#import "DropDownListItemCell.h"


@implementation SearchTypeViewController
@synthesize displayData;
@synthesize popoverController;
@synthesize delegate;
@synthesize typeFull;
@synthesize currentPopoverCellIndex;

#pragma mark -
#pragma mark Initialization

- (id)initWithStyle:(UITableViewStyle)style {
    // Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
    if ((self = [super initWithStyle:style])) {
        
        if ([self respondsToSelector:@selector(setPreferredContentSize:)]) {
            self.preferredContentSize = CGSizeMake(140, 4 * DROP_DOWN_CELL_HEIGHT_IPHONE - 1);
        } else {
            self.contentSizeForViewInPopover = CGSizeMake(140, 4 * DROP_DOWN_CELL_HEIGHT_IPHONE - 1);
        }
    }
    return self;
}

#pragma mark -
#pragma mark View lifecycle

- (void)viewDidLoad {
    [super viewDidLoad];

    if([self.tableView respondsToSelector:@selector(setSeparatorInset:)])
    {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero]; 
    }
	self.view.backgroundColor = [UIColor clearColor];
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    if(typeFull == 1) {
        displayData = [NSArray arrayWithObjects:
                       @"Bài hát",
                       @"Ca sĩ",
                       @"Bài hát Hot",
                       @"Bài hát mới",
                       @"1TB nhạc trẻ",
                       @"2TB nhạc trẻ",
                       @"1TB nhạc vàng",
                       @"2TB nhạc vàng",
                       nil];
    }else {
        displayData = [NSArray arrayWithObjects:
                       @"Bài hát",
                       @"Ca sĩ",
                       @"Bài hát Hot",
                       @"Bài hát mới",
                       nil];
    }
}


/*
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}
*/
/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/


#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [displayData count];
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return DROP_DOWN_CELL_HEIGHT_IPHONE;
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"DropDownListItemCell";
    
    DropDownListItemCell *cell = (DropDownListItemCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"DropDownListItemCell" owner:self options:nil] objectAtIndex:0];
    }
    
    // Configure the cell...
	cell.titleLabel.text = [displayData objectAtIndex:[indexPath row]];
	cell.titleLabel.textColor = [UIColor blackColor];
    
    if (currentPopoverCellIndex == indexPath.row) {
        [cell setSelected:true animated:YES];
    }
    return cell;
}


#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.
    [popoverController dismissPopoverAnimated:YES];
    
    if (currentPopoverCellIndex == indexPath.row) {
        return;
    }
    
    currentPopoverCellIndex = indexPath.row;
	if (popoverController && delegate && [delegate respondsToSelector:@selector(dropDownList:didSeletedAtIndex:title:)]) {
        [delegate dropDownList:popoverController didSeletedAtIndex:currentPopoverCellIndex title:[displayData objectAtIndex:indexPath.row]];
    }
}


#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}


- (void)dealloc {
    displayData = nil; 
}

- (instancetype) init
{
    self = [super init];
    if(self)
    {
        currentPopoverCellIndex = -1;
    }
    return self;
}

@end

